<?php
namespace app\wechat\controller;

class Member extends Common
{
    public function index()
    {
        /*PJ非微信本地测试方法*/
        if( is_weixin() ){
            if (input('?param.openid')) {
                session('openid', input('param.openid'));
            }
            if (!session('?openid')) {
                $url = config('new.interface_url') . 'wechat/wechat_oauth/index/scope/snsapi_base';
                exit('<script>window.location.href="'.$url.'"</script>');
            }
        }

        $member_id = session('member_id');

        // 检查是否有合伙人权限
        $partner = db('partner')->where('member_id', $member_id)->find();
        $this->assign('partner', $partner);
        if (!$partner) {
            // 没有申请 转入申请提示页面
            return view('member/audit');
        } else if ($partner['audit_status'] != 2) {
            // 审核状态页面
            $this->redirect('certification/audit_status', ['status' => $partner['audit_status'], 'msg' => $partner['msg']]);
        }

        $member_info = model('Member')->get($member_id);
        if (!$member_info->memberRoleInfo->nickname) {
            $member_info->memberRoleInfo->nickname = session('?nickname') ? session('nickname') : '';
        }
        if (!$member_info->memberRoleInfo->headimgurl) {
            $member_info->memberRoleInfo->headimgurl = session('?headimgurl') ? session('headimgurl') : '';
        }

        $this->assign('member', $member_info->memberRoleInfo);

        // 获取合伙人首页数据
        $param = [
            'member_id' => session('member_id'),
            'terrace' => 'weixin'
        ];
        $data = call_interface('partner_index_data', $param);
        //dump($data);
        if ($data['code'] == 200) {
            $this->assign('data', $data['data']);
            return view();
        }
        return view('public/error');

    }

    // 修改单张图片  （logo  头像）
    public function save_image() {
        $model = input('param.model');
        $field = input('param.field');
        $where['member_id'] = session('member_id');
        if (request()->isAjax()) {
            $image = input('param.image');
            $rs = db($model)->where($where)->update([$field => $image]);
            if ($rs !== false) {
                return json(['code' => 200, 'msg' => 'SUCCESS']);
            } else {
                return json(['code' => 0, 'msg' => 'FAULT']);
            }
        } else {
            $image = db($model)->where($where)->value($field);
            if ($model == 'member_role_info' && !$image) {
                $image = session('?headimgurl') ? session('headimgurl') : '';
            }
            $this->assign('image', $image);
            $this->assign('model', $model);
            $this->assign('field', $field);
        }
        return view();
    }
}
